package com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.analysis.vo.HddcAppZztCountVo;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.repository.entity.HddcWyGeologicalsvyplanningptEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.service.HddcWyGeologicalsvyplanningptService;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.viewobjects.HddcWyGeologicalsvyplanningptQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zyb
 * @date 2020-11-30
 */
@Slf4j
@RestController
@RequestMapping("/hddc/hddcWyGeologicalsvyplanningpts")
public class HddcWyGeologicalsvyplanningptController {
    @Autowired
    private HddcWyGeologicalsvyplanningptService hddcWyGeologicalsvyplanningptService;

    @GetMapping("/queryHddcWyGeologicalsvyplanningpts")
    public RestResponse queryHddcWyGeologicalsvyplanningpts(HttpServletRequest request, HddcWyGeologicalsvyplanningptQueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = hddcWyGeologicalsvyplanningptService.queryHddcWyGeologicalsvyplanningpts(queryParams, curPage, pageSize);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/findAllByhddcWyGeologicalsvyplanningpts")
    public RestResponse findAllByCreateUserAndTaskIdAndProjectIdAndIsValid(String userId, String taskId, String projectId, String isValid) {
        RestResponse response = null;
        try {
            List<HddcWyGeologicalsvyplanningptEntity> list = hddcWyGeologicalsvyplanningptService.findAllByCreateUserAndTaskIdAndProjectIdAndIsValid(userId, taskId, projectId, isValid);
            response = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{uuid}")
    public RestResponse getHddcWyGeologicalsvyplanningpt(@PathVariable String uuid) {
        RestResponse response = null;
        try {
            HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt = hddcWyGeologicalsvyplanningptService.getHddcWyGeologicalsvyplanningpt(uuid);
            response = RestResponse.succeed(hddcWyGeologicalsvyplanningpt);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping
    public RestResponse saveHddcWyGeologicalsvyplanningpt(@RequestBody HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            hddcWyGeologicalsvyplanningptService.saveHddcWyGeologicalsvyplanningpt(hddcWyGeologicalsvyplanningpt);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    @PutMapping
    public RestResponse updateHddcWyGeologicalsvyplanningpt(@RequestBody HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            hddcWyGeologicalsvyplanningptService.updateHddcWyGeologicalsvyplanningpt(hddcWyGeologicalsvyplanningpt);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @DeleteMapping
    public RestResponse deleteHddcWyGeologicalsvyplanningpts(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            hddcWyGeologicalsvyplanningptService.deleteHddcWyGeologicalsvyplanningpts(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(hddcWyGeologicalsvyplanningptService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /***
     * 导出
     * @param response
     * @return
     */
    @GetMapping("/exportFile")
    public RestResponse exportFileYhDisasters(HttpServletResponse response,
                                              @RequestParam("projectName") String projectName, @RequestParam("taskName") String taskName,
                                              @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        RestResponse responseRest = null;
        JSONObject jsonObject = new JSONObject();
        try {
            HddcAppZztCountVo queryParams = new HddcAppZztCountVo();
            queryParams.setProjectName(projectName);
            queryParams.setTaskName(taskName);
            queryParams.setStartTime(startTime);
            queryParams.setEndTime(endTime);
            hddcWyGeologicalsvyplanningptService.exportFile(queryParams, response);
            jsonObject.put("message", "导出成功");
            responseRest = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            responseRest = RestResponse.fail(errorMessage);
        }
        return responseRest;
    }

    /**
     * 外业原数据管理-地质调查数据:
     * 地质调查规划点-点
     */
    @PostMapping("/importDisaster")
    public RestResponse<String> importDisaster(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse<String> restResponse;
        try {
            String s = hddcWyGeologicalsvyplanningptService.importExcel(file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

}